package smecta.client.print.service;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import smecta.client.print.domain.ScpCodeFormModel;
import smecta.client.print.domain.ScpDataAcqModel;
import smecta.client.print.domain.query.PageDTO;
import smecta.client.print.domain.query.ReportQuery;
import smecta.client.print.mapper.ScpDataAcqMapper;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScpDataAcqService extends BaseService<ScpDataAcqModel> {

    @Autowired
    private ScpDataAcqMapper scpDataAcqMapper;


    @Transactional(rollbackFor = Exception.class)
    public void deleteByCodeNum(String codeNum) {
        if (StringUtils.isBlank(codeNum)){
            return;
        }
        Example example = new Example(ScpDataAcqModel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("codeNum",codeNum);
        deleteByExample(example);
    }

    public List<ScpDataAcqModel> getListByCodeNum(String codeNum) {
        if (StringUtils.isBlank(codeNum)){
            return new ArrayList<>();
        }
        Example example = new Example(ScpDataAcqModel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("codeNum",codeNum);
        example.orderBy("boxNum").asc();
        return selectByExample(example);
    }

    public List<ScpDataAcqModel> getListByPerson(String person){
        Example example = new Example(ScpDataAcqModel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("person",person);
        return selectByExample(example);
    }

    public Integer getMaxBoxNum(String text) {
        List<ScpDataAcqModel> list = getListByPerson(text);
        if (CollectionUtils.isEmpty(list)){
            return 0;
        }
        List<ScpDataAcqModel> sortList = list.stream().sorted(Comparator.comparing(ScpDataAcqModel::getCodeNum)
                        .thenComparing(ScpDataAcqModel::getBoxNum).reversed()).toList();
        return sortList.get(0).getBoxNum();
    }


    public String getLastCodeNumListByPerson(String text) {
        List<ScpDataAcqModel> list = getListByPerson(text);
        if (CollectionUtils.isEmpty(list)){
            return "";
        }
        List<ScpDataAcqModel> sortList = list.stream().sorted(Comparator.comparing(ScpDataAcqModel::getCodeNum).reversed()).toList();
        return sortList.get(0).getCodeNum();
    }

    public PageDTO<ScpDataAcqModel> page(ReportQuery query, List<String> list) {
        PageDTO<ScpDataAcqModel> pageDTO = new PageDTO<>();
        pageDTO.setPageSize(query.getPageSize());
        pageDTO.setPageNo(query.getPageNo());
        int count = scpDataAcqMapper.getPageCount(query.getFuzzySearch(), query,list);
        pageDTO.setTotalNum(count);
        if (count <= 0){
            return pageDTO;
        }
        pageDTO.setList(scpDataAcqMapper.getPageList(query.getFuzzySearch(), query,list));
        return pageDTO;
    }
}
